cmake_minimum_required(VERSION 3.8)
enable_language(CXX)
enable_language(C)

option(WITH_TCMALLOC	"Use tcmalloc from gperftools" ON)
option(WITH_JEMALLOC	"Use jemalloc from FaceBook" OFF)
if(CMAKE_SYSTEM_NAME MATCHES "Android")
	option(WITH_ENTROPY		"Use getentropy() from glibc 2.25 or above" OFF)
	message (STATUS "build on Android")
else()
	option(WITH_ENTROPY		"Use getentropy() from glibc 2.25 or above" ON)
endif()

set(CMAKE_BUILD_TYPE "Release")
#set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CC_FLAGS "-std=c11")
set(CMAKE_CXX_FLAGS "-std=c++17")

if(CMAKE_C_COMPILER MATCHES "clang" OR CMAKE_SYSTEM_NAME MATCHES "Android")
add_definitions( "-Ofast -pthread -funroll-loops" )
message (STATUS "Using clang compiler ...")
else()
add_definitions( "-Ofast -march=native -pthread -funroll-loops" )
message (STATUS "Using compiler ... ${CMAKE_C_COMPILER}")
endif()
add_definitions( "-Wpedantic -Wall -Wuninitialized" )
#add_definitions( "-DWITH_ADD_2007bl" )
#add_definitions( "-DWITH_DBL_2004hmv" )
#add_definitions( "-DWITH_MONT_D" )
#add_definitions( "-DEXHAUSTIVE_TEST_ORDER" )
#add_definitions( "-DNO_HALF_N" )
#add_definitions( "-DWITH_ECDSA" )
#add_definitions( "-DWITH_SM2_MULTSTEP" )
#add_definitions( "-DNO_U64ZERO" )
#add_definitions( "-DNO_CONDITIONAL_COPY" )
#add_definitions( "-DWITH_BIGNUMZ" )
#add_definitions( "-DWITH_BN_PROD" )
# WITH_GETENTROPY only for glibc 2.25 or above (CentOS8...)

if(WITH_ENTROPY)
add_definitions( "-DWITH_GETENTROPY" )
endif()

include(FindPkgConfig)

enable_testing()


if (WITH_TCMALLOC)
	pkg_check_modules (LIBTCMALLOC libtcmalloc_minimal)
endif()

if (WITH_JEMALLOC AND NOT CMAKE_CROSSCOMPILING)
	pkg_check_modules (JEMALLOC jemalloc)
endif()

project(eccTest)

include_directories(.)
include_directories(../include)
pkg_check_modules (BENCH benchmark)
#find_package(GTest REQUIRED)

if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	message(STATUS "current platform: FreeBSD")
	include_directories(/usr/local/include)
	pkg_check_modules (LIBGTEST REQUIRED gtest)
else()
	message(STATUS "linux default w/ gtest")
	set(LIBGTEST_LDFLAGS "gtest")
endif()

add_executable(ecc.test
		test.cc
		../ecc/sm2p.c
		testData.hpp
        )
if (LIBTCMALLOC_FOUND)
	message (STATUS "Using tcmalloc...")
	target_link_libraries(ecc.test "${LIBGTEST_LDFLAGS}" "${LIBTCMALLOC_LDFLAGS}")
elseif(JEMALLOC_FOUND)
	message (STATUS "Using jemalloc...")
	target_link_libraries(ecc.test "${LIBGTEST_LDFLAGS}" "${JEMALLOC_LDFLAGS}")
else()
	target_link_libraries(ecc.test "${LIBGTEST_LDFLAGS}")
endif()

if (BENCH_FOUND)
		message(STATUS "Build benchmark for vli ECC")
add_executable(ecc.bench
        bench.cc
		../ecc/sm2p.c
		testData.hpp
        )
if (LIBTCMALLOC_FOUND)
	message (STATUS "Using tcmalloc...")
	target_link_libraries(ecc.bench benchmark pthread "${LIBTCMALLOC_LDFLAGS}")
elseif(JEMALLOC_FOUND)
	message (STATUS "Using jemalloc...")
	target_link_libraries(ecc.bench benchmark pthread "${JEMALLOC_LDFLAGS}")
else()
	target_link_libraries(ecc.bench benchmark pthread)
endif()
endif()

add_test(test ./ecc.test)
SET_TESTS_PROPERTIES(test PROPERTIES ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE = 1")
